package cn.cdqf.web;

import cn.cdqf.bean.TMajor;
import cn.cdqf.common.ResultResponse;
import cn.cdqf.service.MajorService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class MajorServlet {

    @Autowired
    private MajorService majorService;

    @RequestMapping("/getMajors")
    @ResponseBody
    public List<TMajor> getMajors(){
        List<TMajor> majors = majorService.getMajors();
        return majors;
    }


    @RequestMapping("/queryMajors")
    @ResponseBody
    public List<TMajor> queryMajors(){
        List<TMajor> majors = majorService.queryMajors();
        return majors;
    }

    @RequestMapping("/deleteMajors")
    @ResponseBody
    public ResultResponse deleteMajors(int id){
        int i = majorService.deleteMajors(id);
        ResultResponse resultResponse =null;
        if (i>0){
            resultResponse = new ResultResponse(100,"删除成功");
        }else {
            resultResponse = new ResultResponse(200,"删除失败");
        }
        return resultResponse;
    }

    @RequestMapping("/queryMajorById")
    @ResponseBody
    public TMajor queryMajorById(int id){
        TMajor tMajor= majorService.queryMajorById(id);
        return tMajor;
    }

    @RequestMapping("/updataMajor/{id}")
    @ResponseBody
    public ResultResponse updateMajor(TMajor tMajor,int id){
        int i = majorService.updateMajor(tMajor,id);
        ResultResponse resultResponse =null;
        if (i>0){
            resultResponse = new ResultResponse(100,"更新成功");
        }else {
            resultResponse = new ResultResponse(200,"更新失败");
        }
        return resultResponse;
    }

    @RequestMapping("/addMajor")
    @ResponseBody
    public ResultResponse addMajor(TMajor tMajor){
        int i = majorService.addMajor(tMajor);
        System.out.println(tMajor);
        ResultResponse resultResponse =null;
        if (i>0){
            resultResponse = new ResultResponse(100,"更新成功");
        }else {
            resultResponse = new ResultResponse(200,"更新失败");
        }
        return resultResponse;
    }


    @RequestMapping("/getMajorName")
    @ResponseBody
    public String getMajorName(){
        List<TMajor> majorName = majorService.getMajorName();
        return JSON.toJSONString(majorName);
    }
}
